/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.Application;
import ejb.ApplicationFacade;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Danilo
 */
@ManagedBean
@RequestScoped
public class ApplicationManagedBean {

    @EJB
    private ApplicationFacade af;
    private List<String> res;
    private String app;
    private String searchText;
    private int selectedId;

    /** Creates a new instance of ApplicationManagedBean */
    public ApplicationManagedBean() {
    }
    
    @PostConstruct
    private void setSearchList(){
        res = af.getApplicationsNames();
    }

    /**
     * @return the af
     */
    public ApplicationFacade getAf() {
        return af;
    }
    public Application getSelectedApplication(){
        return af.find(selectedId);
    }
    
    public int getSelectedId() {
        return selectedId;
    }

    /**
     * @param selectedId the selectedId to set
     */
    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }

    /**
     * @return the searchText
     */
    public String getSearchText() {
        return searchText;
    }

    /**
     * @param searchText the searchText to set
     */
    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }
    
    public String viewResult(){
       return "applicationView.xhtml?faces-redirect=true&amp;id="+af.getApplicationIdForName(searchText);
    }
    
    public List<String> complete(String query){
        List<String> result = new ArrayList<String>();
        for (String name : res) {
            if(name.toLowerCase().startsWith(query.toLowerCase()))
            {
                result.add(name);
            }
        }
        res = result;
        return result;
    }

    /**
     * @return the app
     */
    public String getApp() {
        return app;
    }

    /**
     * @param app the app to set
     */
    public void setApp(String app) {
        this.app = app;
    }
}
